package cn.kgc.spring.factorys;

import com.mysql.jdbc.Driver;
import org.springframework.beans.factory.FactoryBean;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @author 课工场
 * @date 2024/4/28
 * @description 实例工厂  获取数据库连接对象
 */
public class ConnectionFactory implements FactoryBean<Connection> {

    /*
     * 将ConnectionFactory类托管到spring容器后  获取对象时会自动调用该方法
     * 将该方法的返回值作为获取的对象
     * */
    @Override
    public Connection getObject() throws Exception {

        Driver driver = new Driver();
        DriverManager.registerDriver(driver);
        String username = "root";
        String password="root";
        String url = "jdbc:mysql:///java2424";
        Connection connection = DriverManager.getConnection(url, username, password);
        return connection;
    }


    // 设置返回值的类型
    @Override
    public Class<?> getObjectType() {
        return Connection.class;
    }
}
